﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;


namespace KiemBao
{
    public partial class Form1 : Form 
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Node A;
   
        private ArrayList AllStates;
        private Node B;
        private bool DrapXeOfSV;
        private bool DrapCaoThangCle;
        private Graphics g;
        private int iCapDo;
        private float k;
        private ArrayList m_arrG;
        private ArrayList m_arrQ;
        private ArrayList m_arrV;
        private float Max_X;
        private float Max_Y;
        private ArrayList Obstacles;
        private ArrayList SA;
        private ArrayList SAB;
        private ArrayList SB;
        private ArrayList States;
        public ArrayList G
        {
            get
            {
                return this.m_arrG;
            }
            set
            {
                this.m_arrG = value;
            }
        }
        public ArrayList Q
        {
            get
            {
                return this.m_arrQ;
            }
            set
            {
                this.m_arrQ = value;
            }
        }
        public ArrayList V
        {
            get
            {
                return this.m_arrV;
            }
            set
            {
                this.m_arrV = value;
            }
        }

        public int CapDo(int iCapDo)
    {
        return iCapDo ;
    }
        private ArrayList Exclusion(ArrayList StateActive, ArrayList ObstaclesActive)
        {
            ArrayList list = new ArrayList();
            list = StateActive;
            if (ObstaclesActive.Count == 0)
            {
                return StateActive;
            }
            foreach (Node state in ObstaclesActive)
            {
                if (StateActive.Contains(state))
                {
                    list.Remove(state);
                }
            }
            return list;
        }
        private void CreatSucc(Node State)
        {
            this.CapDo(this.iCapDo);
            State.SUCCESS = new ArrayList();
            if (State.X < (this.iCapDo - 1))
            {
                float num = State.X + 1f;
                State.SUCCESS.Add(num.ToString() + "-" + State.Y.ToString());
            }
            if (State.X > 0f)
            {
                float num2 = State.X - 1f;
                State.SUCCESS.Add(num2.ToString() + "-" + State.Y.ToString());
            }
            if (State.Y < (this.iCapDo - 1))
            {
                float num3 = State.Y + 1f;
                State.SUCCESS.Add(State.X.ToString() + "-" + num3.ToString());
            }
            if (State.Y > 0f)
            {
                float num4 = State.Y - 1f;
                State.SUCCESS.Add(State.X.ToString() + "-" + num4.ToString());
            }
        }
        private bool CheckObstacles(Node StateObs)
        {
            bool flag = true;
            if ((StateObs.NAME == this.A.NAME) || (StateObs.NAME == this.B.NAME))
            {
                flag = false;
            }
            if ((StateObs.NAME == this.A.NAME) || (StateObs.NAME == this.B.NAME))
            {
                flag = false;
            }
            foreach (Node state in this.Obstacles)
            {
                if (state.NAME == StateObs.NAME)
                {
                    flag = false;
                }
            }
            return flag;
        }
        private void Paint()
        {
            this.CapDo(iCapDo);
            //this.iCapDo = 11;//(this.tbGame.Value * 10) - (5 * (this.tbGame.Value - 1));
            this.AllStates = TinhToan.MathGame(this.iCapDo);
            if (this.Obstacles.Count == 0)
            {
                this.States = this.Exclusion(this.AllStates, this.Obstacles);
            }
            
            this.k = (this.Max_X - 28f) / ((float)this.iCapDo);
            this.PicPaint.Refresh();
            this.PaintAll();
            this.G.Add(this.B);
            this.CreatSucc(this.A);
            this.CreatSucc(this.B);
            ArrayList list = new ArrayList();
            ArrayList list2 = new ArrayList();
            CacThuatToan.BFS_ChiPhiThapNhat(list2, list, this.SA, this.States, this.G);
            ArrayList list3 = new ArrayList();
            int num = 0;
            foreach (Node state in this.States)
            {
                if (this.B.NAME == state.NAME)
                {
                    list3.Add(state);
                }
            }
            bool flag = false;
            while (!flag)
            {
                try
                {
                    if (Node.Previous((Node)list3[num]) == null)
                    {
                        flag = true;
                    }
                    else
                    {
                        list3.Add(Node.Previous((Node)list3[num]));
                        num++;
                    }
                    continue;
                }
                catch
                {
                    this.txtKetQua.Text = "";
                    return;
                }
            }
            if (list3.Count > 1)
            {
                this.V = new ArrayList();
                this.V = list3;
                this.txtKetQua.Text = "";
                for (int i = this.V.Count - 1; i >= 0; i--)
                {
                    if (i != 0)
                    {
                        Node state2 = (Node)this.V[i];
                        this.txtKetQua.Text = this.txtKetQua.Text + state2.NAME + " Chạy Tới ";
                    }
                    else
                    {
                        Node state3 = (Node)this.V[i];
                        this.txtKetQua.Text = this.txtKetQua.Text + state3.NAME;
                    }
                }
                this.V.RemoveAt(this.V.Count - 1);
                this.V.RemoveAt(0);
                DoThi.State(this.V, this.g, this.k, "LanDuong.jpg");
                this.Obstacles.Clear();
            }
            else
            {
                this.txtKetQua.Text = "Bít Đường Rồi !!!Nghỉ Học Thôi!!!";
            }
        }
        private void PaintAll()
        {
            this.PicPaint.Refresh();
            DoThi.Frame(this.g, this.Max_X, this.Max_Y);
            DoThi.Matrix(this.g, this.Max_X, this.Max_Y, this.k);
            DoThi.State(this.SA, this.g, this.k, "XeOfSV.jpg");
            DoThi.State(this.SB, this.g, this.k, "CaoThangCle.jpg");
            DoThi.State(this.Obstacles, this.g, this.k, "LoCot.jpg");
        }
      
        private void Reset()
        {
            this.Obstacles.Clear();
            this.SAB.Clear();
            this.A = new Node();
            this.B = new Node();
            this.States.Clear();
            this.AllStates.Clear();
            this.Q.Clear();
            this.G.Clear();
            this.V.Clear();
            this.SA.Clear();
            this.SB.Clear();
            this.iCapDo = this.CapDo(iCapDo);//(this.tbGame.Value * 10) - (5 * (this.tbGame.Value - 1));
            this.k = (this.Max_X - 24f) / ((float)this.iCapDo);
            this.AllStates = TinhToan.MathGame(this.iCapDo);
            this.States = this.Exclusion(this.AllStates, this.Obstacles);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.g = this.PicPaint.CreateGraphics();
            this.Max_X = this.PicPaint.Width;
            this.Max_Y = this.PicPaint.Height;
            this.Obstacles = new ArrayList();
            this.SAB = new ArrayList();
            this.A = new Node();
            this.B = new Node();
            this.States = new ArrayList();
            this.AllStates = new ArrayList();
            this.SA = new ArrayList();
            this.SB = new ArrayList();
            this.Q = new ArrayList();
            this.G = new ArrayList();
            this.V = new ArrayList();
            this.txtKetQua.Text = "Ban Ko Chọn Level Chương Trình Chạy Với Level Mật Định";
            this.iCapDo = 6;
            this.CapDo(iCapDo);
            this.Paint();
            this.States = this.Exclusion(this.AllStates, this.Obstacles);
            this.Paint();
        }
        private void PicPaint_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.DrapXeOfSV || this.DrapCaoThangCle)
            {
                if (this.DrapXeOfSV)
                {
                    foreach (Node state in this.AllStates)
                    {
                        if (((((state.X * this.k) + 24f) < e.X) && ((((state.X * this.k) + 24f) + this.k) > e.X)) && ((((state.Y * this.k) + 24f) < e.Y) && ((((state.Y * this.k) + 24f) + this.k) > e.Y)))
                        {
                            this.A = state;
                            this.SA.Add(this.A);
                        }
                    }
                    DoThi.State(this.SA, this.g, this.k, "XeOfSV.jpg");
                    this.DrapXeOfSV = false;
                }
                else
                {
                    foreach (Node state2 in this.AllStates)
                    {
                        if (((((state2.X * this.k) + 24f) < e.X) && ((((state2.X * this.k) + 24f) + this.k) > e.X)) && ((((state2.Y * this.k) + 24f) < e.Y) && ((((state2.Y * this.k) + 24f) + this.k) > e.Y)))
                        {
                            this.B = state2;
                            this.SB.Add(this.B);
                        }
                    }
                    DoThi.State(this.SB, this.g, this.k, "CaoThangCle.jpg");
                    this.DrapCaoThangCle = false;
                }
            }
            else
            {
                foreach (Node state3 in this.AllStates)
                {
                    if ((((((state3.X * this.k) + 24f) < e.X) && ((((state3.X * this.k) + 24f) + this.k) > e.X)) && ((((state3.Y * this.k) + 24f) < e.Y) && ((((state3.Y * this.k) + 24f) + this.k) > e.Y))) && this.CheckObstacles(state3))
                    {
                        this.Obstacles.Add(state3);
                    }
                }
                DoThi.State(this.Obstacles, this.g, this.k, "LoCot.jpg");
                this.States = this.Exclusion(this.AllStates, this.Obstacles);
            }
        }
        private void ptbXeOfSV_Click(object sender, EventArgs e)
        {
            this.DrapXeOfSV = true;
            this.DrapCaoThangCle = false;
            this.SA.Clear();
            this.PaintAll();
        }
        private void ptbCaoThangCle_Click(object sender, EventArgs e)
        {
            
            this.DrapXeOfSV = false;
            this.DrapCaoThangCle = true;
            this.SB.Clear();
            this.PaintAll();
        }
      
        private void SV_Click(object sender, EventArgs e)
        {
            this.DrapXeOfSV = true;
            this.DrapCaoThangCle = false;
            this.SA.Clear();
            this.PaintAll();
        }
        private void Chay_Click(object sender, EventArgs e)
        {
            this.Paint();
            this.Reset();
         
        }
        private void LamMoi_Click(object sender, EventArgs e)
        {
            this.PicPaint.Refresh();
            this.txtKetQua.Text = "";
            this.Reset();
            DoThi.Frame(this.g, this.Max_X, this.Max_Y);
            DoThi.Matrix(this.g, this.Max_X, this.Max_Y, this.k);
        }
        private void Thoat_Click(object sender, EventArgs e)
        {
            
            DialogResult dlr = MessageBox.Show("Bạn Muốn Thoát Àh? ",
                "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dlr == DialogResult.Yes)
                this.Close();
            //Application.Exit();
        }
        private void Info_Click(object sender, EventArgs e)
        {
            this.txtKetQua.Text = "Nhóm CDTH08B :Phạm Bảo Thanh_MSSV:306081200 +Nguyễn Văn Tân_MSSV:306081198+Nguyễn Văn Thuận_MSSV:306081208+Lê Thanh Tùng_MSSV:306081223+Võ Ngọc Tỷ_MSSV:306081226";
        }
        private void GioiThieu_Click(object sender, EventArgs e)
        {
            this.txtKetQua.Text = "Đây là chương trình giúp bạn tìm đường dễ dàng wa mấy cái Lô Cốt đáng ghét để đến trường Cao Thắng thân yêu !!! (^_^)>>>>Tham Khảo Tài Liệu Trên Mạng <<<<";
        }

        private void CaoThang_Click(object sender, EventArgs e)
        {
            //// KIEM TRA XEM CO THUOC Node nao chua
            //bool b_In = false;
            //foreach (Node node in m_arrQ)
            //{
            //    if (node.X + node.R + 30 >= e.X && node.X - node.R - 30 <= e.X && node.Y + node.R + 30 >= e.Y && node.Y - node.R - 30 <= e.Y)
            //    {
            //        MessageBox.Show("Trong");
            //        b_In = true;
            //    }
            //}
            //if (b_In == false)
            //{
               
            //}
            this.DrapXeOfSV = false;
            this.DrapCaoThangCle = true;
            this.SB.Clear();
            this.PaintAll();
        }

        private void lv1_Click(object sender, EventArgs e)
        {
            this.iCapDo = 6;
            this.CapDo(iCapDo);
            this.Paint();
        }

        private void Lv2_Click(object sender, EventArgs e)
        {
            this.iCapDo = 11;
            this.CapDo(iCapDo);
            this.Paint();
        }

        private void Lv3_Click(object sender, EventArgs e)
        {
            this.iCapDo = 16;
            this.CapDo(iCapDo);
            this.Paint();
        }

        private void Lv4_Click(object sender, EventArgs e)
        {
            this.iCapDo = 21;
            this.CapDo(iCapDo);
            this.Paint();
        }

        private void Lv5_Click(object sender, EventArgs e)
        {
            this.iCapDo = 26;
            this.CapDo(iCapDo);
            this.Paint();
        }

        private void Lv6_Click(object sender, EventArgs e)
        {
            this.iCapDo = 31;
            this.CapDo(iCapDo);
            this.Paint();
        }

        private void Lv7_Click(object sender, EventArgs e)
        {
            this.iCapDo = 36;
            this.CapDo(iCapDo);
            this.Paint();
        }

        private void Lv8_Click(object sender, EventArgs e)
        {
            this.iCapDo = 41;
            this.CapDo(iCapDo);
            this.Paint();
          

        }

        private void Lv9_Click(object sender, EventArgs e)
        {
            this.iCapDo = 46;
            this.CapDo(iCapDo);
            this.Paint();
        }

        private void Lv10_Click(object sender, EventArgs e)
        {
            this.iCapDo = 51;
            this.CapDo(iCapDo);
            this.Paint();
        }

   
 
    }
}